package com.java110.mall.shop.bmo.shopVipLevel.impl;

import com.java110.dto.coupon.CouponConfDto;
import com.java110.dto.vipUser.ShopVipLevelDto;
import com.java110.intf.acct.IAccountInnerServiceSMO;
import com.java110.intf.acct.IShopVipAccountInnerServiceSMO;
import com.java110.intf.shop.ICouponConfInnerServiceSMO;
import com.java110.intf.shop.IShopVipLevelInnerServiceSMO;
import com.java110.intf.shop.IShopVipUserInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.mall.shop.bmo.shopVipLevel.IGetShopVipLevelBMO;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getShopVipLevelBMOImpl")
public class GetShopVipLevelBMOImpl implements IGetShopVipLevelBMO {

    @Autowired
    private IShopVipLevelInnerServiceSMO shopVipLevelInnerServiceSMOImpl;

    @Autowired
    private IShopVipAccountInnerServiceSMO shopVipAccountInnerServiceSMOImpl;

    @Autowired
    private IShopVipUserInnerServiceSMO shopVipUserInnerServiceSMOImpl;


    @Autowired
    private IUserV1InnerServiceSMO userInnerServiceSMOImpl;

    /**
     * 账户 内部服务类
     */
    @Autowired
    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;
    @Autowired
    private ICouponConfInnerServiceSMO couponConfInnerServiceSMOImpl;

    /**
     * @param shopVipLevelDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(ShopVipLevelDto shopVipLevelDto) {


        int count = shopVipLevelInnerServiceSMOImpl.queryShopVipLevelsCount(shopVipLevelDto);

        List<ShopVipLevelDto> shopVipLevelDtos = null;
        if (count > 0) {
            shopVipLevelDtos = shopVipLevelInnerServiceSMOImpl.queryShopVipLevels(shopVipLevelDto);
            refreshCouponConf(shopVipLevelDtos);
        } else {
            shopVipLevelDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) shopVipLevelDto.getRow()), count, shopVipLevelDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    private void refreshCouponConf(List<ShopVipLevelDto> shopVipLevelDtos) {


        if (ListUtil.isNull(shopVipLevelDtos)) {
            return;
        }
        List<String> vipLevelIds = new ArrayList<>();
        for (ShopVipLevelDto shopVipLevelDto : shopVipLevelDtos) {
            vipLevelIds.add(shopVipLevelDto.getVipLevelId());
        }
        CouponConfDto couponConfDto = new CouponConfDto();
        couponConfDto.setVipLevelIds(vipLevelIds.toArray(new String[vipLevelIds.size()]));
        List<CouponConfDto> couponConfDtos = couponConfInnerServiceSMOImpl.queryCouponConfs(couponConfDto);

        List<CouponConfDto> tmpCouponConfDto = null;
        for (ShopVipLevelDto tempShopVipLevelDto : shopVipLevelDtos) {
            tmpCouponConfDto = new ArrayList<>();
            for (CouponConfDto tmpCouponConfDtos : couponConfDtos) {
                if (tempShopVipLevelDto.getVipLevelId().equals(tmpCouponConfDtos.getVipLevelId())) {
                    tmpCouponConfDto.add(tmpCouponConfDtos);
                }
            }
            tempShopVipLevelDto.setCouponConfDtos(tmpCouponConfDto);
        }
    }



}
